<?php 
class Exam_Model_DbTable_Answers extends Engine_Db_Table{
    protected $_rowClass = 'Exam_Model_Answer';
    
    public function getAnswersByQuestions($questions,$isStatistic = false,$exam_id = 0){
        $answers = array();
        $select = $this->select();
        if(is_numeric($questions)){
            $select->where('question_id = ?',$questions);
        }
        if(is_array($questions) && !empty($questions)){
            $select->where('question_id in (?)',$questions);
        }
        $select->order('question_id');
        $select->order('order');
        
        $statistic = null;
        if($isStatistic == true){
            $useranswerTable = Engine_Api::_()->getDbtable('useranswers','exam');
            $statistic = $useranswerTable->getStaticQuestionAnswer($exam_id);
        }
        
        $rows = $this->fetchAll($select);
        if($rows){
            if($statistic){
                foreach($rows as $item){
                    if(!isset($answers[$item->question_id])) $answers[$item->question_id] = array();
                    $answers[$item->question_id]['result'][$item->answer_id]= $item;
                    if($item->result) $answers[$item->question_id]['correct'][] = $item->answer_id;
                    $answers[$item->question_id]['group'] = $statistic[$item->question_id];
                    
                } 
            }
            else{
                foreach($rows as $item){
                    if(!isset($answers[$item->question_id])) $answers[$item->question_id] = array();
                    $answers[$item->question_id]['result'][$item->answer_id]= $item;
                    if($item->result) $answers[$item->question_id]['correct'][] = $item->answer_id;
                    
                } 
            }
            
            
        }
        
        
        
        return $answers;
                       
                        
        
    }
    public function getTrueAnswer($options){
        $examdetailTable = Engine_Api::_()->getDbtable('examdetails','exam');
        $examdetaiName = $examdetailTable->info('name');
            
        $select = $this->select()
                       ->setIntegrityCheck(false)  
                       ->where('result = 1'); 
        
        if(isset($options['exam_id']) && !empty($options['exam_id'])){
            
            $select->from(array('answers'=>$this->info('name')),array('*')) 
                   ->join(array('examdetails'=>$examdetaiName),
                        "examdetails.question_id = answers.question_id",
                        array('score')
                   )
                   ->where('examdetails.exam_id = ?',$options['exam_id'])
                   ->where('answers.result = 1');
            if(isset($options['question_id']) && !empty($options['question_id'])){
                $select->where('examdetails.question_id = ?',$options['question_id']);
            }       
            $rows = $this->fetchAll($select);
            
            $result = array();
            foreach($rows as $item){
                $result[$item->question_id]['correct'][] = $item->answer_id;
                $result[$item->question_id]['score'] = $item->score;
            }
            
            return $result;
        }
        
        
    }
    
    public function getAnswersByQuestion($question){
        $answers = array();
        $select = $this->select();
        if(is_numeric($question)){
            $select->where('question_id = ?',$question);
        }
        else if($question instanceof Exam_Model_Question){
            $select->where('question_id = ?', $question->getIdentity());
        }
        $select->order('order');

        $rows = $this->fetchAll($select);
        return $rows;
    }
}
    
?>